package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: chunlin
 * @date: 2020/10/8
 * @description 角色controller
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Reference
    private RoleService roleService;

    /**
     * 添加
     *
     * @param role
     * @param menuIds
     * @param permissionIds
     * @return
     */
    @RequestMapping("/roleAdd")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result roleAdd(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds) {
        log.info("[角色-添加]data:{},menuIds:{},permissionIds:{}", role, menuIds, permissionIds);
        //rpc调用
        roleService.roleAdd(role, menuIds, permissionIds);
        return new Result(true, "新增角色成功");
    }

    /**
     * 查询角色数据
     *
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.info("[角色-根据id查询]id:{}", id);
        //rpc调用
        Role role = roleService.findById(id);
        return new Result(true, "查询角色成功", role);
    }

    /**
     * 根据角色id查询菜单id集合
     *
     * @return
     */
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        log.info("[根据角色id查询菜单id集合]id:{}", id);
        //rpc调用
        List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
        return new Result(true, "查询成功", menuIds);
    }

    /**
     * 根据角色id查询权限id集合
     *
     * @return
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        log.info("[根据角色id查询权限id集合]id:{}", id);
        //rpc调用
        List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);

        return new Result(true,"查询成功",permissionIds);
    }

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[角色-分页查询]data:{}", queryPageBean);
        //rpc调用
        PageResult pageResult = roleService.findPage(queryPageBean);
        return new Result(true, "查询成功", pageResult);
    }

    /**
     * 查询所有权限
     *
     * @return
     */
    @GetMapping("/findAllPermission")
    public Result findAllPermission() {
        //rpc调用
        Set<Permission> permissions = roleService.findAllPermission();
        return new Result(true, "查询权限成功",permissions);
    }

    /**
     * 查询所有菜单
     *
     * @return
     */
    @GetMapping("/findAllMenu")
    public Result findAllMenu() {
        //rpc调用
        LinkedHashSet<Menu> menus = roleService.findAllMenu();
        return new Result(true, "查询菜单成功", menus);
    }

    /**
     * 编辑
     *
     * @return
     */
    @RequestMapping("/roleEdit")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result roleEdit(@RequestBody Role role,
                           Integer[] menuIds,
                           Integer[] permissionIds) {
        log.info("[角色-编辑]data:{},menuIds:{},permissionIds:{}", role, menuIds, permissionIds);
        //rpc调用
        roleService.roleEdit(role, menuIds, permissionIds);
        return new Result(true, "编辑角色成功");
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */

    @RequestMapping("/roleDelete")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result roleDelete(Integer id) {
        log.info("[角色-根据id删除]id:{}", id);
        //rpc调用
        roleService.roleDelete(id);
        return new Result(true, "角色删除成功");
    }
}
